/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.config.Config;
import com.manage.model.ChannelAppInfo;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

/**
 * @author jun.huang
 *
 */
public class ChannelAppInfoServiceImpl extends AbstractService implements ChannelAppInfoService
{
	private Logger logger;
	
	public ChannelAppInfoServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.BOSSSECTION, Config.KEY_DBCP_NAME);
	}
	
	@Override
	public ChannelAppInfo getChannelAppInfo(int id) 
	{
		String sql = "select ac.*,av.appversionnumber appversionnumber from t_appchannelapkinfo ac,t_appversioninfo av where av.id=ac.appversionid and ac.appversionid in (" +
						"select appversionid from t_appversionterrace where terraceid = (" +
							"select id from t_terrace where code='kpay')) and id=" + String.valueOf(id) + "";
		ModelGenerator<ChannelAppInfo> dbcpmg = new ModelGenerator<ChannelAppInfo>(ChannelAppInfo.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<ChannelAppInfo> getChannelAppInfos()
	{
		String sql = "select ac.*,av.appversionnumber appversionnumber from t_appchannelapkinfo ac,t_appversioninfo av where av.id=ac.appversionid and ac.appversionid in (" +
						"select appversionid from t_appversionterrace where terraceid = (" +
							"select id from t_terrace where code='kpay')) order by id";
		logger.info("[method:getChannelAppInfos] [sql]: " + sql);
		ModelGenerator<ChannelAppInfo> dbcpmg = new ModelGenerator<ChannelAppInfo>(ChannelAppInfo.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public List<ChannelAppInfo> getChannelAppInfos(int channelid) {
		String sql = "select ac.*,av.appversionnumber appversionnumber from t_appchannelapkinfo ac,t_appversioninfo av where av.id=ac.appversionid and ac.appversionid in (" +
						"select appversionid from t_appversionterrace where terraceid = (" +
							"select id from t_terrace where code='kpay')) and channelid='" + String.valueOf(channelid) + "' order by id";
		logger.debug("sql: " + sql);
		ModelGenerator<ChannelAppInfo> dbcpmg = new ModelGenerator<ChannelAppInfo>(ChannelAppInfo.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean saveChannelAppInfo(Account operator, String type, ChannelAppInfo app)
	{
		/*if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			app.setId(getTableId("channelappinfo"));
			sql = 
				"insert into channelappinfo(id,cpid,channelappnumber,appnumber,appname,apppackage,appstatus,createuser,createdate,appversion" +
				",appmd5,appiconname,reiconname,appiconsrc,appfilename,refilename,appfilesrc,appentry,channelid,projectid) " +
				"values(" + String.valueOf(app.getId()) + ",'" + app.getCpid() + "','" + app.getChannelapknumber() + "','" + 
				encodeDb(app.getAppnumber()) + "','" + encodeDb(app.getAppname()) + "','" +
						encodeDb(app.getApppackage()) + "','0','" + app.getCreateuser()+ "','" +
						app.getCreatedate() + "','" + app.getAppversion()+ "'"+
						",'" + app.getAppmd5()+ "'"+
						",'" + app.getAppiconname()+ "'"+
						",'" + app.getReiconname()+ "'"+
						",'" + app.getAppiconsrc()+ "'"+
						",'" + app.getAppfilename()+ "'"+
						",'" + app.getRefilename()+ "'"+
						",'" + app.getAppfilesrc()+ "'"+
						",'" + app.getAppentry()+ "'"+
						",'" + app.getChannelid()+ "'"+
						",'" + app.getProjectid()+ "'"+
						
						")";
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = 
				"update channelappinfo set appname='" + encodeDb(app.getAppname()) + "'," +
						"apppackage='" + encodeDb(app.getApppackage()) + "', " +
						"appversion='" + encodeDb(app.getAppversion()) + "' " +
						
						",appmd5='" + encodeDb(app.getAppmd5()) + "' " +
						",appiconname='" + encodeDb(app.getAppiconname()) + "' " +
						",reiconname='" + encodeDb(app.getReiconname()) + "' " +
						",appiconsrc='" + encodeDb(app.getAppiconsrc()) + "' " +
						",appfilename='" + encodeDb(app.getAppfilename()) + "' " +
						",refilename='" + encodeDb(app.getRefilename()) + "' " +
						",appfilesrc='" + encodeDb(app.getAppfilesrc()) + "' " +
						",appentry='" + encodeDb(app.getAppentry()) + "' " +
						",channelid='" +app.getChannelid() + "' " +
						",projectid='" + app.getProjectid() + "' " +
						
				" where id=" + app.getId() + "";
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "update channelappinfo set appstatus='1' where id=" + String.valueOf(app.getId()) + "";
		}
		else if (type.equalsIgnoreCase("online"))
		{
			sql = "update channelappinfo set appstatus='0' where id=" + String.valueOf(app.getId()) + "";
		}
		else if (type.equalsIgnoreCase("offline"))
		{
			sql = "update channelappinfo set appstatus='1' where id=" + String.valueOf(app.getId()) + "";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				ChannelAppInfo lo = null;
				if (!type.equals("delete"))
					lo = getChannelAppInfo(app.getId());
				logTableOperate("channelappinfo", type, operator, lo);
				return true;
			}
		}	*/	
		return false;
	}

}
